{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简单线性回归教程（二）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### —— 用梯度下降法求解线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAGrlJREFUeJzt3XuMXVd1x/HvyniASdoyhIyiZIyxEZEtwCSGEQS5QsQBHCBKLINoUmjDo4qqopZCa7BV1EIFtZFbAf2jVCmvVKEhaRIcHi0mio2QojpojAMmEJfwiONJggclpiIZyNhZ/eOemxnP3Me597z22ef3kayZe+bOnX3PeNbZe+219zF3R0RE4nVG1Q0QEZFiKdCLiEROgV5EJHIK9CIikVOgFxGJnAK9iEjkFOhFRCKnQC8iEjkFehGRyK2ougEA55xzjq9evbrqZoiI1MrBgwd/6e4T/Z4XRKBfvXo109PTVTdDRKRWzOyBNM9T6kZEJHJ9A72Zfc7MjpvZDxYd221m95nZ983sy2Y2vuhrO8zsfjM7Ymabi2q4iIikk6ZH/wXgsiXH7gBe4u4vBf4X2AFgZi8CrgJenHzPv5jZSG6tFRGRgfUN9O7+beDRJce+6e4nk4cHgJXJ51cCX3L337r7z4D7gVfk2F4RERlQHjn6dwH/nXw+CTy46GvHkmMiIlKRTFU3ZvY3wEngi0N877XAtQCrVq3K0gwRkY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es>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "points = genfromtxt('data.csv', delimiter=',')\n",
    "N = len(points)\n",
    "\n",
    "# 提取 x，y\n",
    "x = array(points[:,0])\n",
    "y = array(points[:,1])\n",
    "\n",
    "# 用plt做出散点图\n",
    "plt.scatter(x,y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义线性模型 y = mx + b\n",
    "\n",
    "def compute_cost(points, b, m):\n",
    "    total_cost = 0\n",
    "    \n",
    "    N = len(points)\n",
    "    \n",
    "    # 计算所有误差平方的和 sum(y-mx-b)^2\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        \n",
    "        total_cost += (y - m * x - b) ** 2\n",
    "    \n",
    "    return total_cost/float(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 定义模型的超参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 包括：步长（学习速率），初始点，迭代次数\n",
    "learning_rate = 0.0001\n",
    "initial_b = 0\n",
    "initial_m = 0\n",
    "num_iteration = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 定义梯度下降算法函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradient_descent(points, initial_b, initial_m, learning_rate, num_iteration):\n",
    "    b = initial_b\n",
    "    m = initial_m\n",
    "    \n",
    "    # 用一个list记录所有的损失函数值\n",
    "    cost_list = []\n",
    "    \n",
    "    for i in range(num_iteration):\n",
    "        cost_list.append(compute_cost(points, b, m))\n",
    "        b, m = step_grad_desc(b, m, array(points), learning_rate)\n",
    "    \n",
    "    return [b, m, cost_list]\n",
    "\n",
    "def step_grad_desc(current_b, current_m, points, learning_rate):\n",
    "    m_grad = 0\n",
    "    b_grad = 0\n",
    "    \n",
    "    N = len(points)\n",
    "    \n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        m_grad += (current_m * x + current_b - y) * x\n",
    "        b_grad += current_m * x + current_b - y\n",
    "    \n",
    "    m_update = current_m - learning_rate * m_grad * (2/float(N))\n",
    "    b_update = current_b - learning_rate * b_grad * (2/float(N))\n",
    "    \n",
    "    return b_update, m_update"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 测试：运行梯度下降函数来计算最优m和b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "final m 1.4774173755483797\n",
      "final b 0.02963934787473238\n"
     ]
    }
   ],
   "source": [
    "b, m , cost_list = gradient_descent(points, initial_b, initial_m, learning_rate, num_iteration)\n",
    "\n",
    "print(\"final m: \", m)\n",
    "print(\"final b: \", b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "final cost:  112.65585181499748\n"
     ]
    }
   ],
   "source": [
    "print(\"final cost: \", compute_cost(points,b,m))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. 画出损失函数随着迭代下降的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vc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xj4cb7fV0REzlZwncAiIpIfBT0bqJl1AwcW8BINwPEclbPU6bs4m76PM/RdnK0Yvo+ku884iqagA2ChzKxtNlOiRoG+i7Pp+zhD38XZovR96BSQiEhEKQBERCKq2APgnrALKCD6Ls6m7+MMfRdni8z3UdR9ACIiMr1iPwIQEZFpFGUAmNmNZvaCmXWa2R1h1xMmM2sys5+b2W4z22Vmnwy7prCZWdzMfm1mPwq7lrCZWa2ZPWhmz5vZHjN7U9g1hcnM/mvw7+Q5M/uemS0Lu6bFVHQBkHXTmZuATcDvm9ni3letsI0Dn3L3TcDVwO0R/z4APgnsCbuIAvFV4Cfu/nrgCiL8vZjZGuC/ACl3fwOZ6WpuCbeqxVV0AYBuOnMWd+9y945g+SSZf+CRvQeDma0F3gV8K+xawmZmNcBW4F4Adx91975wqwpdCVBhZiVAJXA05HoWVTEGgG46Mw0zawauBB4Pt5JQfQX4NJAOu5ACcDHQDXw7OCX2LTOrCruosLj7EeBLwEGgC+h395+GW9XiKsYAkCmYWTXwT8CfuPurYdcTBjN7N3DM3dvDrqVAlAAtwDfd/UrgFBDZPjMzS5A5W3AxsBqoMrP/FG5Vi6sYA2DGm85EjZmVkvnl/113/37Y9YTozcB7zGw/mVODbzezvwu3pFAdBg67++QR4YNkAiGq3gG85O7d7j4GfB/4DyHXtKiKMQB005ksZmZkzvHucfe/DrueMLn7ne6+1t2byfz/4mfuXtR/4V2Iu78MHDKz1wVN1wK7QywpbAeBq82sMvh3cy1F3ime9/sBLDbddOY8bwY+BDxrZk8FbZ8J7ssg8sfAd4M/ll4EPhJyPaFx98fN7EGgg8zouV9T5FcF60pgEZGIKsZTQCIiMgsKABGRiFIAiIhElAJARCSiFAAiIhGlABARiSgFgIhIRCkAREQi6v8DHOOXLX7hiQQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(cost_list)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "迭代5次之后，就达到了极小值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. 画出拟合曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3X2UFfWd5/H3l6bB1iS2D4wjTQhk4oAPrWJYNYeY+DRBhWiLT6DOaOJZshtX87AhQtadGY0OsORETXYmiZuMMieKICpGWWU8ag4TdzADouITE41PtBrIYKtoi033b/+ouv1wb93nqrpVdT+vczj0rVt9749q+nN/9a1vVZlzDhERya5RjR6AiIhES0EvIpJxCnoRkYxT0IuIZJyCXkQk4xT0IiIZp6AXEck4Bb2ISMYp6EVEMm50owcAcOCBB7pJkyY1ehgiIqmyadOmPzrnxpVbLxFBP2nSJDZu3NjoYYiIpIqZvVrJeirdiIhkXNmgN7N/NLPtZvbMsGXLzOwFM3vazO4xs/Zhzy0ysxfNbKuZzYxq4CIiUplKZvS3AqflLXsIOMI5dyTw78AiADM7DJgLHO5/zz+YWUtooxURkaqVDXrn3HpgZ96yf3bO7fEfbgAm+F+fBdzhnNvtnHsZeBE4NsTxiohIlcKo0X8VeMD/ugN4fdhz2/xlIiLSIHV13ZjZ/wD2ALfV8L3zgfkAEydOrGcYIiJVWbO5m2XrtvJGTy/j29tYMHMKXdOyOyeteUZvZpcCs4GL3NBtqrqBTw5bbYK/rIBz7mbn3HTn3PRx48q2gYqIhGLN5m4W3b2F7p5eHNDd08uiu7ewZnNgVGVCTUFvZqcB3wXOdM59M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gcAq4CJeFdoPd85t7NBw4yVmR2Nd5B+DPB74Cv4vzM02fbwP/xfAz7tnHvHXxbb/w0FvYhIxql0IyKScQp6EZGMU9CLiGScgl5EJOMU9CIiGaegFxHJOAW9iEjGKehFRDLu/wOrWJ+kr4OAigAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "\n",
    "# 针对每一个x，求出预测值\n",
    "y_pred = m * x + b\n",
    "\n",
    "plt.plot(x, y_pred, c='r')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
